/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-1-14 上午11:20 Created by dwzhang
 */
package com.jade.bss.api.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jade.framework.base.util.ResponseUtils;
import com.jade.modules.config.ConfigEntity;
import com.jade.modules.config.ConfigException;
import com.jade.modules.config.ConfigUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 基础配置管理接口控制器。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-1-14 上午11:20
 */
@RestController
@RequestMapping ("/config")
public class ConfigController
{
    @RequestMapping (value = "/list.json")
    public void list(@RequestParam (value = "ownerType") String ownerType,
                     @RequestParam (value = "ownerId") String ownerId,
                     @RequestParam (value = "group") String group,
                     HttpServletResponse response)
            throws Exception
    {
        ResponseUtils.writeSuccessResult(response, ConfigUtils.listEntity(ownerType, ownerId, group));
    }

    @RequestMapping (value = "/add.json")
    public void add(@RequestBody ConfigEntity entity,
                    HttpServletResponse response)
            throws Exception
    {
        try {
            ConfigUtils.addEntity(entity);
            ResponseUtils.writeSuccessResult(response);
        }
        catch (ConfigException e) {
            ResponseUtils.writeErrorResult(response, e);
        }
    }

    @RequestMapping (value = "/set.json")
    public void set(@RequestParam (value = "ownerType") String ownerType,
                    @RequestParam (value = "ownerId") String ownerId,
                    @RequestParam (value = "group") String group,
                    @RequestBody List<ConfigEntity> entities,
                    HttpServletResponse response)
            throws Exception
    {
        try {
            ConfigUtils.setEntity(ownerType, ownerId, group, entities);
            ResponseUtils.writeSuccessResult(response);
        }
        catch (ConfigException e) {
            ResponseUtils.writeErrorResult(response, e);
        }
    }

    @RequestMapping (value = "/edit.json")
    public void edit(@RequestBody ConfigEntity entity,
                     HttpServletResponse response)
            throws Exception
    {
        try {
            ConfigUtils.updateEntity(entity);
            ResponseUtils.writeSuccessResult(response);
        }
        catch (ConfigException e) {
            ResponseUtils.writeErrorResult(response, e);
        }
    }

    @RequestMapping (value = "/remove.json")
    public void remove(@RequestParam (value = "ids") long[] ids,
                       HttpServletResponse response)
            throws Exception
    {
        try {
            ConfigUtils.removeEntity(ids);
            ResponseUtils.writeSuccessResult(response);
        }
        catch (ConfigException e) {
            ResponseUtils.writeErrorResult(response, e);
        }
    }
}
